<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>角色信息显示界面</title>
  <title>角色的数据表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../res/layui/css/layui.css"  media="all">
</head>
<body>
 <script src="../res/js/jquery-3.4.1.min.js"></script>
 
<table class="layui-hide" id="test" lay-filter="test"></table>
 
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    <button class="layui-btn layui-btn-sm" lay-event="add">新增数据</button>
	<button class="layui-btn layui-btn-sm" lay-event="delAll">批量删除</button>
  </div>
</script>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
              
<script type="text/html" id="switchTpl">
  <input type="checkbox" name="sex" value="{{d.roleId}}" lay-skin="switch" lay-text="是|否" lay-filter="sexDemo" {{ d.roleValidity == 1 ? 'checked' : '' }}>
</script>
          
<script src="../res/layui/layui.js" charset="utf-8"></script>
 
<script>
var table2 = null;
layui.use('table', function(){
  var table = layui.table
  ,form = layui.form;;
  
  table2=table.render({
    elem: '#test'
    ,url:'../Role/findAllPage.action'
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
      title: '提示'
      ,layEvent: 'LAYTABLE_TIPS'
      ,icon: 'layui-icon-tips'
    }]
    ,title: '角色数据表'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'roleId', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
      ,{field:'roleName', title:'用户名', width:120, edit: 'text'}
      ,{field:'roleNote', title:'备注', width:120, edit: 'text'}
      ,{field:'roleValidity', title:'是否有效', width:150,  templet: '#switchTpl'}
      ,{field:'roleSort', title:'排序', width:80, edit: 'text', sort: true}
      ,{field:'rolePeople', title:'创建人', width:100}
      ,{field:'roleTime', title:'创建时间', width:120,templet: "<div>{{layui.util.toDateString(d.roleTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
    ]]
    ,page: true
  });
  //是否选中事件
  form.on('switch(sexDemo)', function(obj){
 		var look=0; 
	  //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
	    if(obj.elem.checked==true){
	    	look=1;
	    }else{
	    	look=0;
	    }
	   	var url="../Role/updateRolevalidity.action";
	   	var data={roleId:this.value,roleValidity:look};
	   	$.post(url,data,function(obj){
	   		if(obj.state==1){
	   			if(look==1){
	   		    	layer.msg("已显示");
	   		    }else{
	   		    	layer.msg("已隐藏");
	   		    }
	   		}
	   		table2.reload();
	   	});
	  });
  
  //头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        layer.alert(JSON.stringify(data));
      break;
      case 'add'://新增数据
    	  layer.open({
			  type: 2,
			  title: '角色界面编辑',
			  shadeClose: true,
			  shade: 0.8,
			  area: ['580px', '90%'],
			  content: 'roleinsert.html', //iframe的url
			  end:function(){
				  table2.reload();
			  }
			}); 
      break;
      case 'delAll':
        layer.msg("批量删除");
      break;
      
      //自定义头工具栏右侧图标 - 提示
      case 'LAYTABLE_TIPS':
        layer.alert('这是工具栏右侧自定义的一个图标按钮');
      break;
    };
  });
  
  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    //console.log(obj)
    if(obj.event === 'del'){
      if(data.roleValidity==1){
    		var url="../Role/updateRolevalidity.action";
    	   	var data={roleId:data.roleId,roleValidity:0};
    	   	$.post(url,data,function(obj){
    	   		if(obj.state==1){
    	   		    layer.msg("已隐藏");
    	   		}
    	   		table2.reload();
    	   	});
      }
      
    } else if(obj.event === 'edit'){
      layer.prompt({
        formType: 2
        ,value: data.email
      }, function(value, index){
        obj.update({
          email: value
        });
        layer.close(index);
      });
    }
  });
});
</script>

</body>
</html>